"use strict";
const express = require('express');
const ArticleHrefAdapter = require('../adapter/ArticleHrefAdapter')
const ArticleDetailAdapter = require('../adapter/ArticleDetailAdapter')
const JsonAdapter = require('../adapter/JsonAdapter');
const JsonpAdapter = require('../adapter/JsonpAdapter');
const SpecialAdapter = require('../adapter/SpecialAdapter');
const logger = require('../conf/logger');
const router = express.Router();


/* GET home page. */
router.post('/test', async function(req, res, next) {
	let page = req.body;
	logger.debug(page);
	let resBodyJson;

	try{
		if(1 == page.asSpecial ||SpecialAdapter.specialSites.indexOf(page.pageSite.trim()) != -1){
			//处理特殊情况
            let data = await new SpecialAdapter(page, true).execute();
            if(data.contents && data.contents.length > 0){
                resBodyJson =  {
                    code:data.code,
                    data:data.contents
                }
            } else {
                resBodyJson = {
                    code: 402,
                    errMsg:'获取到内容为空'
                }
			}

		}else if(1 == page.ctype){
			//内容为文本
			const articleBody = await new ArticleHrefAdapter(page, true).execute();

			if(articleBody.code != 200){
				resBodyJson = articleBody;
			}else{
				if(articleBody.contents && articleBody.contents.length > 0){

					if(page.followLink == 0){
						resBodyJson =  {
							code:200,
							data:articleBody.contents
						}
					}else{
						const detailBody = await new ArticleDetailAdapter({
							source:articleBody.pageTitle,
							sourceUrl:articleBody.url,
							pageSite:articleBody.contents[0].href,
							pageTitle:articleBody.contents[0].title,
							titleRule:articleBody.titleRule,
							contentRule:articleBody.contentRule,
							encoding:articleBody.encoding,
							cates:articleBody.cates,
							sourceTime:articleBody.sourceTime,
							group:'1'
						}, true).execute();

						if(detailBody.code != 200){
							resBodyJson = detailBody;
						}else{
							let data = [];
							for(let i = 0; i < articleBody.contents.length; i++){
								data.push({
									title:i == 0? detailBody.title : articleBody.contents[i].title,
									href:articleBody.contents[i].href,
									content: i==0?detailBody.content:''
								})
							}

							resBodyJson = {
								code: 200,
								data: data
							}
						}
					}

				}else{
					resBodyJson = {
						code: 402,
						errMsg:'获取到内容为空'
					}
				}
			}

		}else if(2 == page.ctype){
			//内容为json
			const jsonBody = await new JsonAdapter(page, true).execute();
			if(jsonBody.code != 200){
				resBodyJson = jsonBody;
			}else{
				resBodyJson =  {
					code:200,
					data:jsonBody.contents
				}
			}
		}else if(3 == page.ctype){
			//内容为jsonp
			const jsonBody = await new JsonpAdapter(page, true).execute();
			if(jsonBody.code != 200){
				resBodyJson = jsonBody;
			}else{
				resBodyJson =  {
					code:200,
					data:jsonBody.contents
				}
			}
		}
	}catch (e){
		console.error(e);
		resBodyJson = {
			code:555,
			errMsg: e.message
		}
	}

	res.json(resBodyJson);
})


module.exports = router;
